package com.qf.qflivehou.service.serviceImpl;

import com.qf.qflivehou.config.RedisKey;
import com.qf.qflivehou.mapper.QfCourseDao;
import com.qf.qflivehou.pojo.reqVo.ShopCartReq;
import com.qf.qflivehou.pojo.respVo.ResponseData;
import com.qf.qflivehou.pojo.vo.LiveUser;
import com.qf.qflivehou.pojo.vo.QfCourse;
import com.qf.qflivehou.service.ShopCartService;
import com.qf.qflivehou.service.UserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Mr.Liu
 * @date 2022/1/12 17:20
 */
@Service
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private QfCourseDao qfCourseDao;

    @Override
    public ResponseData addShopCart(ShopCartReq shopCartReq, HttpServletRequest request) {
        //判断用户登录状态
        ResponseData responseData = userService.findUserByToken(request);
        if(responseData.getCode()==1){
            return responseData;
        }
        //如果用户正常登录,可操作购物车
        LiveUser liveUser = (LiveUser) responseData.getData();
        //从redis中获取,判断当前课程是否在购物车已经存在
        Object val = redisTemplate.opsForHash().get(RedisKey.SHOPCART_KEY + liveUser.getUserId().toString(), shopCartReq.getCourseid().toString());
        if(val==null){
            //为null,说明不存在,添加到redis中
            QfCourse qfCourse = qfCourseDao.queryById(shopCartReq.getCourseid());
            redisTemplate.opsForHash().put(RedisKey.SHOPCART_KEY + liveUser.getUserId().toString(),shopCartReq.getCourseid().toString(),qfCourse);
            return new ResponseData(0,"添加成功",null,null);
        }else {
            return new ResponseData(1,"添加课程重复",null,null);
        }
    }

    @Override
    public ResponseData findShopCartByToken(HttpServletRequest request) {
        //判断用户登录状态
        ResponseData responseData = userService.findUserByToken(request);
        if(responseData.getCode()==1){
            return responseData;
        }
        //如果用户正常登录,可进行操作
        LiveUser liveUser = (LiveUser) responseData.getData();
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.SHOPCART_KEY + liveUser.getUserId().toString());
        if(aBoolean){
            Map entries = redisTemplate.opsForHash().entries(RedisKey.SHOPCART_KEY + liveUser.getUserId().toString());
            return new ResponseData(0,"查询成功",entries,null);
        }
        return new ResponseData(1,"购物车为空,请先添加课程",null,null);
    }

    @Override
    public ResponseData delShop(ShopCartReq shopCartReq,HttpServletRequest request) {
        //判断用户登录状态
        ResponseData responseData = userService.findUserByToken(request);
        if(responseData.getCode()==1){
            return responseData;
        }
        //如果用户正常登录,可进行操作
        LiveUser  liveUser = (LiveUser) responseData.getData();
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.SHOPCART_KEY + liveUser.getUserId().toString());
        if(aBoolean){
            Long size = redisTemplate.opsForHash().size(RedisKey.SHOPCART_KEY + liveUser.getUserId().toString());
            //删除有两种情况,判断是否为最后一个课程
            if(size>1){
                //购物车有多个课程,删除某一个课程
                redisTemplate.opsForHash().delete(RedisKey.SHOPCART_KEY + liveUser.getUserId().toString(),shopCartReq.getCourseid().toString());
            }else {
                //是最后一个课程
                redisTemplate.delete(RedisKey.SHOPCART_KEY + liveUser.getUserId().toString());
            }
            return new ResponseData(0,"删除成功",null,null);
        }
        return new ResponseData(1,"购物车为空,请添加课程",null,null);
    }
}
